package com.system.controller;
import com.system.service.PinkeService;
import com.system.utils.BaseController;
import com.system.utils.excelutil.OutPutTxt;
import com.system.utils.httpexcuteutils.Goget;
import com.system.utils.poutils.PinkeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
 * @author zch
 */
@Controller
@RequestMapping("/executor")
public class HttpController extends BaseController {
	@Resource(name = "pinkeServiceImpl")
	private PinkeService pinkeService;
	/**
	 * sendChargeIndex
	 */
	@RequestMapping(value = "/sendChargeIndex")
	public String sendChargeIndex(HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
		return "pinke/http";
	}
	/**
	 * showResult
	 */
	@RequestMapping("/executorCharge")
	public void executorCharge(Integer execType, HttpServletRequest request, HttpServletResponse response, Model model) {
		// 设置日期格式
		SimpleDateFormat df = new SimpleDateFormat("yyyy-05-26 HH:mm:ss");
		System.out.println("execType:" + execType);
		List<Map<Object, Object>> needHandle = pinkeService.selectAllHandlInfo(130);
		System.out.println("needHandle:" + needHandle.size());
		for (Map<Object, Object> m : needHandle) {
			if ("0".equals(m.get("currstatues").toString())) {
				System.out.println(m);
				String opFare = m.get("opfare").toString();
				String transactionId = m.get("transId").toString();
				String opTime = df.format(new Date());
				String userNo = m.get("userNO").toString();
				String cardNo = m.get("cardNo").toString();
				String busNo = m.get("busNo").toString();
				// {currstatues=0, transId=4200000327201905277798646476, userNO=170324, busNo=13943, opfare=10000, cardNo=1815, cardNO=1815, userId=508336, username=高浩然}
				// String opFare, String transactionId, String opTime, String userNo
				System.out.println("信息装配完毕:" + opFare + transactionId + opTime + userNo);
				Integer result = Goget.httpCharge(opFare, transactionId, opTime, userNo);
				System.out.println("cardNo:" + cardNo);
				System.out.println("睡眠开始");
				OutPutTxt.Log("E:", "charge.txt", "opFare:" + opFare + "  " + "transactionId:" + transactionId + "  " + "opTime:" + opTime + "  " + "userNo:" + userNo + "  " + "cardNo:" + cardNo + "  " + "busNo:" + busNo);
				OutPutTxt.Log("E:", "cardNo.txt", "cardNo:" + cardNo);
				OutPutTxt.Log("E:", "busNo.txt", "busNo:" + busNo);
				try {
					int up = pinkeService.updateStateByBusNo(Integer.valueOf(busNo));
					System.out.println("更新处理状态:" + up);
					if (up > 0) {
						System.out.println("睡眠开始");
						Thread.sleep(10000);
						System.out.println("睡眠结束开始下一个");
					}
					else {
						break;
					}
				}
				catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		writeJson(response, 1);
	}
}
